//使用promise的方式来封装wx原生api
export default {
  //获取用户授权的所有相关信息
  getSetting: function () {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        withSubscriptions: true,
        success: (res) => {
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  },
  //打开用户授权界面
  openSetting: function () {
    return new Promise((resolve, reject) => {
      wx.openSetting({
        withSubscriptions: true,
        success: (res) => {
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  },
  //获取wx收获地址
  chooseAddress: function () {
    return new Promise((resolve, reject) => {
      wx.chooseAddress({
        success: (res) => {
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  },
  //showToast封装
  showToast: function (msg) {
    return new Promise((resolve, reject) => {
      wx.showToast({
        title: msg,
        icon: 'none',
        success: (res) => {
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  },

  //获取微信code
  login: function () {
    return new Promise((resolve, reject) => {
      //执行小程序登录
      wx.login({
        timeout: 1000,
        success(res) {
          resolve(res)
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  },
  //调起微信支付
    // wx.requestPayment({
    //   nonceStr: 'nonceStr',
    //   package: 'package',
    //   paySign: 'paySign',
    //   timeStamp: 'timeStamp',
    //   success (res) { 支付成功的回调函数 },
    //   fail (res) { 支付失败的回调函数 }
    // })
  requestPayment: function (pay) {
    return new Promise((resolve, reject) => {
      //执行小程序登录
      wx.requestPayment({
        ...pay,
        success(res) {
          console.log("res",res)
          resolve(res)
        },
        fail: (err) => {
          console.log("err",err)
          reject(err)
        }
      })
    })
  }
}